package com.lb.pdm.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lb.pdm.common.constants.RedisConstants;
import com.lb.pdm.common.result.GraceJSONResult;
import com.lb.pdm.entity.FloatingPopulation;
import com.lb.pdm.entity.PersonInfo;
import com.lb.pdm.entity.SysUser;
import com.lb.pdm.entity.bo.FloatingPopulationBO;
import com.lb.pdm.entity.vo.FloatingPopulationVO;
import com.lb.pdm.mapper.FloatingPopulationMapper;
import com.lb.pdm.mapper.PersonInfoMapper;
import com.lb.pdm.service.FloatingPopulationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 流动人口表 服务实现类
 * </p>
 *
 * @author pdm
 * @since 2025-10-29
 */
@Service
public class FloatingPopulationServiceImpl extends ServiceImpl<FloatingPopulationMapper, FloatingPopulation> implements FloatingPopulationService {
    @Autowired
    private FloatingPopulationMapper floatingPopulationMapper;
    @Autowired
    private PersonInfoMapper personInfoMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public GraceJSONResult inputFloatingPopulation(FloatingPopulationBO floatingPopulationBO, String token) {
        FloatingPopulation floatingPopulation = new FloatingPopulation();
        BeanUtils.copyProperties(floatingPopulationBO, floatingPopulation);

        String idCard = floatingPopulationBO.getIdCard();
        LambdaQueryWrapper<PersonInfo> personInfoQuery = new LambdaQueryWrapper<>();
        personInfoQuery.eq(PersonInfo::getIdCard, idCard);
        PersonInfo personInfo = personInfoMapper.selectOne(personInfoQuery);
        if (personInfo == null) {
            return GraceJSONResult.errorMsg("人员信息不存在");
        }

        floatingPopulation.setPersonId(personInfo.getId());

        floatingPopulation.setRegisterDate(new Date());
        floatingPopulation.setStatus((byte)1);
        floatingPopulation.setCreateTime(new Date());
        floatingPopulation.setUpdateTime(new Date());

        String sysUserString = redisTemplate.opsForValue().get(RedisConstants.USER_TOKEN_PREFIX + token);
        SysUser sysUser = JSON.parseObject(sysUserString, SysUser.class);
        floatingPopulation.setCreateBy(sysUser.getId());
        floatingPopulation.setUpdateBy(sysUser.getId());

        int insertRes = floatingPopulationMapper.insert(floatingPopulation);
        if (insertRes <= 0) {
            return GraceJSONResult.errorMsg("添加流动人口失败");
        }

        return GraceJSONResult.ok();
    }

    @Override
    public List<FloatingPopulationVO> query(String name, String idCard, Byte residenceType, Integer pageNum, Integer pageSize) {
        int start = (pageNum - 1) * pageSize;
        return floatingPopulationMapper.query(name, idCard, residenceType, start, pageSize);
    }
}
